package com.mall.shopping.services;

import com.mall.shopping.IHomeService;
import com.mall.shopping.converter.PanelContentConverter;
import com.mall.shopping.converter.PanelConverter;
import com.mall.shopping.dal.entitys.Panel;
import com.mall.shopping.dal.entitys.PanelContentItem;
import com.mall.shopping.dal.persistence.PanelContentMapper;
import com.mall.shopping.dal.persistence.PanelMapper;
import com.mall.shopping.dto.HomePageResponse;
import com.mall.shopping.dto.PanelContentItemDto;
import com.mall.shopping.dto.PanelDto;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;


@Service
public class IHomeServiceImp implements IHomeService {

    @Autowired
    PanelMapper panelMapper;
    @Autowired
    PanelContentMapper panelContentMapper;
    @Autowired
    PanelConverter panelConverter;
    @Autowired
    PanelContentConverter panelContentConverter;

    @Override
    public HomePageResponse homepage() {


        List<Panel> panels = panelMapper.selectAll();
        Set<PanelDto> panelDtos = new HashSet<>();
        for (Panel panel : panels) {

            List<PanelContentItemDto> panelContentItemDtos = new ArrayList<>();


            int id = panel.getId();

            List<PanelContentItem> panelContentItems =
                    panelContentMapper.selectPanelContentAndProductWithPanelId(id);

            for (PanelContentItem panelContentItem : panelContentItems) {


                PanelContentItemDto panelContentItemDto =
                        panelContentConverter.panelContent2Dto(panelContentItem);

                panelContentItemDtos.add(panelContentItemDto);


            }


            PanelDto panelDto = panelConverter.panel2Dto(panel);

            panelDto.setPanelContentItems(panelContentItemDtos);

            panelDtos.add(panelDto);


        }

        HomePageResponse homePageResponse = new HomePageResponse();


        homePageResponse.setPanelContentItemDtos(panelDtos);
        return  homePageResponse;

    }
}